<?php

namespace App\Tools;

use Illuminate\Support\Facades\Cache;

class Token2 extends State
{
    protected $cache;

    protected $token;

    protected $uid;

    const TTl = 9000;


    private function getToken()
    {
        if (! $this->token) {
            $this->token = 'token:'.unique_id();
        }
        return $this->token;
    }


    private function setToken($token)
    {
        $this->token = $token;
        return $this;
    }

    private function setUser()
    {
        Cache::put($this->getToken(),$this->getUid(), self::TTl);
        return $this;
    }

    private function getUid()
    {
        if ($this->uid > 0 && is_numeric($this->uid)) {
            throw new \Exception('请正确设置用户id');
        }
        return $this->uid;
    }

    protected function refresh($token)
    {
        $uid = Cache::get($token);
        if(!$uid)
            return false;
        Cache::put($token,$uid, self::TTl);
        return true;
    }

//    public static function __callStatic($method,$param)
//    {
//        if(self::$instance == null){
//            self::$instance = new self();
//        }
//        self::$instance->$method(... $param);
//
//    }
}
